package com.sx.ad.service.impl;

import com.sx.ad.constant.CommonStatus;
import com.sx.ad.constant.Constants;
import com.sx.ad.dao.AdPlanRepository;
import com.sx.ad.dao.AdUserRepository;
import com.sx.ad.entity.AdPlan;
import com.sx.ad.entity.AdUser;
import com.sx.ad.exception.AdException;
import com.sx.ad.service.IAdPlanService;
import com.sx.ad.utils.CommonUtils;
import com.sx.ad.vo.AdPlanGetRequest;
import com.sx.ad.vo.AdPlanRequest;
import com.sx.ad.vo.AdPlanResponse;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author zkx
 * @Date 2024/8/30 14:21
 */
@Service
public class AdPlanServiceImpl implements IAdPlanService {

	private AdUserRepository adUserRepository;

	private AdPlanRepository adPlanRepository;

	@Override
	@Transactional
	public AdPlanResponse createAdPlan(AdPlanRequest request) throws AdException {
		if (!request.createValidate()) {
			throw new AdException(Constants.ErrorMsg.REQUEST_PARAM_ERROR);
		}

		// 确保关联user对象是存在的
		Optional<AdUser> adUser = adUserRepository.findById(request.getUserId());
		if (!adUser.isPresent()){
			throw new AdException(Constants.ErrorMsg.CAN_NOT_FIND_RECORD);
		}

		AdPlan oldPlan = adPlanRepository.findByIdAndUserId(request.getId(), request.getUserId());
		if (oldPlan != null){
			throw new AdException(Constants.ErrorMsg.SAME_NAME_PLAN_ERROR);
		}

		AdPlan newAdPlan = adPlanRepository.save(
				new AdPlan(request.getUserId(), request.getPlanName(),
						CommonUtils.parseStringDate(request.getStartDate()),
						CommonUtils.parseStringDate(request.getEndDate())
				)
		);

		return new AdPlanResponse(newAdPlan.getId(), newAdPlan.getPlanName());
	}

	@Override
	public List<AdPlan> getAdPlanByIds(AdPlanGetRequest request) throws AdException {
		if (!request.validate()) {
			throw new AdException(Constants.ErrorMsg.REQUEST_PARAM_ERROR);
		}

		return adPlanRepository.findAllByIdInAndUserId(
				request.getIds(), request.getUserId()
		);
	}

	@Override
	public AdPlanResponse updateAdPlan(AdPlanRequest request) throws AdException {
		if (!request.updateValidate()) {
			throw new AdException(Constants.ErrorMsg.REQUEST_PARAM_ERROR);
		}

		AdPlan plan = adPlanRepository.findByIdAndUserId(
				request.getId(), request.getUserId()
		);
		if (plan == null) {
			throw new AdException(Constants.ErrorMsg.CAN_NOT_FIND_RECORD);
		}

		if (request.getPlanName() != null) {
			plan.setPlanName(request.getPlanName());
		}
		if (request.getStartDate() != null) {
			plan.setStartDate(
					CommonUtils.parseStringDate(request.getStartDate())
			);
		}
		if (request.getEndDate() != null) {
			plan.setEndDate(
					CommonUtils.parseStringDate(request.getEndDate())
			);
		}

		plan.setUpdateTime(new Date());
		plan = adPlanRepository.save(plan);

		return new AdPlanResponse(plan.getId(), plan.getPlanName());
	}

	@Override
	public void deleteAdPlan(AdPlanRequest request) throws AdException {

		if (!request.deleteValidate()) {
			throw new AdException(Constants.ErrorMsg.REQUEST_PARAM_ERROR);
		}

		AdPlan plan = adPlanRepository.findByIdAndUserId(
				request.getId(), request.getUserId()
		);
		if (plan == null) {
			throw new AdException(Constants.ErrorMsg.CAN_NOT_FIND_RECORD);
		}

		plan.setPlanStatus(CommonStatus.INVALID.getStatus());
		plan.setUpdateTime(new Date());
		adPlanRepository.save(plan);
	}
}
